const {
  $Toast
} = require('../../utils/iview/base/index');
const Tools = require('../../utils/modules/Tools');
import NetWork from '../../utils/modules/NetWork';

const app = getApp();

var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  data: {
    type:'',
    ispay: false,
    province: '',
    city: '广州市',
    type: 1,
    banner: null,
    student: null,
    shopList: null,
    //门店展示的数据
    shopname: [],
    order_id: 0,
    auth: false,
    latitude: 23.099994,
    longitude: 113.324520,
    key: app.globalData._key,
    hidden: false,
    nocancel: false,

    isloade: {
    hidden: false
    },
    _baseUrl: app.globalData._baseUrl
  },


  onLoad: function(e) {
    // console.log(e ,"e数组的数据")
    this.setData({
      order_id: e.id

    })
    // 有支付/取消/订单详情接口，为什么只有调取addpage接口呢，
    // 因为界面一开始就已经有调用addpage接口的数据用来显示页面的信息，而支付取消接口没有就不需要在此调用
    this.addpage()
    // var _that = this
    // _that.setTitle('信息确认');
  },

  onShow: function() {
    let _that = this;
    var user = wx.getStorageSync('user');
    _that.setData({
      user: user
    })
  },


 

  evaluate() {
    wx.navigateTo({
      url: '../evaluate/evaluate',
    })
  },

  // bindtap: function () {
  //   this.setData({
  //     mHidden: false
  //   });
  // },

  // changeModel: function () {
  //   this.setData({
  //     mHidden: true
  //   });
  // },

  // modelCancel: function () {
  //   this.setData({
  //     mHidden: true
  //   });
  // },



  // palys(){
  //   var _that = this;
  //   $Toast({
  //     content:'支付成功!',
  //     type:'success'
  //   })
  //   _that.setData({
  //     type:2
  //   })
  // },
  // cancel(){
  //   wx.showModal({
  //     title: '取消支付',
  //     content: '您确定取消订单吗？若已产生成本费用,则需进行支付！',
  //     success(res){
  //       console.log(res);
  //       if (res.cancel){
  //         $Toast({
  //           content:'取消支付!',
  //           type:'none'
  //         })
  //       }
  //       if(res.confirm){
  //         $Toast({
  //           content: '支付成功!',
  //           type: 'none'
  //         })
  //       }
  //     },
  //   })

  // },



  //订单详情
  addpage() {
    let _that = this;
    var token = wx.getStorageSync('token');
    if (!token) {
      return;
    }

    let net = new NetWork(_that.data._baseUrl)
    net._request({
      url: 'api/v1/order/myOrderInfo',
      data: {
        token: token,
        order_id: _that.data.order_id
      },
      method: 'POST'
    }, '', res => {

      _that.setData({
        order: res.data.data
      })
      console.log(res.data.data, "输出")
      // console.log(_that.data.vehicle,"vehicle")     
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        app.refreshtoken(token);
        setTimeout(() => {
          _that.addpage();
        }, 1500)
      }
    });
  },


  //一键开箱
  num() {
    let _that = this;
    var token = wx.getStorageSync('token');
    if (!token) {
      return;
    }

    let net = new NetWork(_that.data._baseUrl)
    net._request({
      url: 'api/v1/order/opDoor',
      data: {
        token: token,
        order_id: _that.data.order_id
      },
      method: 'POST'
    }, '开柜', res => {

      //开柜成功跳转到详情页
      wx.redirectTo({
        url: '../personal/orderList/orderDetails?id=' + this.data.order_id,
        })


      console.log(this.data.order_id,"order_id")

      console.log(res.data.data, "输出一键开箱")
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        app.refreshtoken(token);
        setTimeout(() => {
          _that.num();
        }, 1500)
      } 
      else if (res.data.error_code == 0) {
        $Toast({
          content: res.data.msg,
        });
       
      } else if (res.data.error_code == 400) {
        $Toast({
          content: res.data.msg,
          type: 'warning'
        });
      }

    });
  },


  // 插队支付的弹框
  // ispay默认是flase,
  pays() {
    var _that = this;
    var ispay = _that.data.ispay;
    _that.setData({
      //！flase就是true,第一次
      ispay: !ispay
    })
  },


  palys1(e) {
    let _that = this;
    //这里是第二次调用 ispay变成了true,那么！ispay:!true 就变成了flase不显示
    _that.pays();
    wx.showModal({
      title: '提示',
      content: '确定要余额？',
      // 余额支付接口是和微信支付接口一样的，余额支付接口要在点击确定后在确认余额支付
      success(res) {
        if (res.confirm) {
          var token = wx.getStorageSync('token');
          var type = e.currentTarget.dataset.value;
          if (!token) {
            return;
          }

          let net = new NetWork(_that.data._baseUrl)
          net._request({
            url: 'api/v1/order/queue_jumping',
            data: {
              token: token,
              order_id: _that.data.order_id,
              jump_type: 1,
              pay_type: type
            },
            method: 'POST'
          }, '支付', res => {
            _that.addpage();
            

          }, res => {
            if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
              app.refreshtoken(token);
              setTimeout(() => {
                _that.num();
              }, 1500)
            }
            // else if (res.data.error_code == 0) {
            //   $Toast({
            //     content: res.data.msg,
            //   });
           

            // } 
            
            else if (res.data.error_code == 400) {
              $Toast({
                content: res.data.msg,
                type: 'warning'
              });
            }

          });
        }

        else if (res.cancel) {
        }
      }
    })
  },


  //插队支付接口
  palys(e) {
    let _that = this;
    _that.pays();
    var token = wx.getStorageSync('token');
    let net = new NetWork(_that.data._baseUrl)
    var type = e.currentTarget.dataset.value;
    console.log(e)
  //   if (pay_type == 1) {
  //     _that.setTitle('微信支付');
  //   }

  //  else if (pay_type == 2) {
  //     _that.setTitle('余额');
  //   }


    net._request({

      url: 'api/v1/order/queue_jumping',
      data: {
        token: token,
        order_id: _that.data.order_id,
        jump_type: 1,
        pay_type: type
      },
      method: 'POST'
    }, '', res => {

      _that.setData({
        payment: res.data.data
      })
      console.log(res.data.data, "支付接口");
      var payment = res.data.data
      //只是支付接口，支付的功能的接口在那里就写在那个接口的res中
      //这个部分是按了确定才出现支付的， 所以写在这里
      wx.requestPayment({
        timeStamp: payment.timeStamp,
        nonceStr: payment.nonceStr,
        package: payment.packages,
        signType: payment.signType,
        paySign: payment.paySign,
        success(res) {
          $Toast({
            content: '支付成功',
            type: 'success'

          });
          _that.addpage();
        },
        fail(res) {
          $Toast({
            content: '支付失败',
            type: 'success'
          });
        }
      })
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        app.refreshtoken(token);
        setTimeout(() => {
          _that.palys();
        }, 1500)
      } else if (res.data.error_code == 400) {
        $Toast({
          content: res.data.msg,
          type: 'warning'
        });
      }


    });
  },

  gohome(){
    wx.reLaunch({
      url: '../index/index',
    })
  },



  intoMap: function (e) {

    var address = e.currentTarget.dataset.value;
    console.log(address)
    var _this = this;
    //调用地址解析接口
    var qqmapsdk = new QQMapWX({
      key: _this.data.key // 必填
    });
    qqmapsdk.geocoder({
      //获取表单传入地址
      address: address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
      success: function (res) {//成功后的回调
        var res = res.result;
        var latitude = res.location.lat;
        var longitude = res.location.lng;


        wx.openLocation({        //所以这里会显示你当前的位置
          latitude: latitude,
          longitude: longitude,
          name: address,
          address: address,
          scale: 28
        })
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        console.log(res);
      }
    })


  },



  cancelbtn() {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定要取消订单吗？',
      success(res) {
        if (res.confirm) {
          that.cancel();
        } else if (res.cancel) {

        }
      }
    })
  },

  //取消订单
  cancel() {
    let _that = this;
    var token = wx.getStorageSync('token');
    let net = new NetWork(_that.data._baseUrl)
    net._request({

      url: 'api/v1/order/editOrder',
      data: {
        token: token,
        order_id: _that.data.order_id,
        status: 9
      },
      method: 'POST'
    }, '取消', res => {

      wx.reLaunch({
        url: '../index/index',
      })
      // wx.redirectTo({
      //   url: '../index/index',
      // })
      //  跳到上一页的代码
      // setTimeout(()=>{
      //   wx.navigateTo({
      //     url: '../index/index',
      //   })
      // },1000)


      //  跳到上一页的代码功能这个通用
      // setTimeout(() => {
      //   wx.navigateBack({
      //     changed: true
      //   });
      // }, 1000)

    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        app.refreshtoken(token);
        setTimeout(() => {
          _that.cancel();
        }, 1500)
      }
    });
  },



  /* 拨打电话 */
  telPhone() {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: ''
    }, res => {

    });
  },


  onReady: function(e) {

  },
  formSubmit() {
    console.log("formSubmit");
    $Toast({
      content: '等待门店接单...',
      type: 'loading'
    })
  },



  /* 修改头部标题 */
  setTitle(str) {
    wx.setNavigationBarTitle({
      title: str,
      success: function() {

      }
    })
  },
  onShareAppMessage(str) {
    return {
      title: str,
      path: this.route
    }
  },


  /* 隐藏分享功能 */
  hideShare() {
    wx.hideShareMenu()
  },
  /* 分享功能 */
  showShare() {
    wx.showShareMenu()
  },
  /* 下拉刷新触发功能 */
  onPullDownRefresh() {
    // 下拉刷新事件
    // wx.startPullDownRefresh
    // wx.stopPullDownRefresh
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)

  },

  /* 开启上拉到底部触发功能 */
  startDonwEven() {
    wx.startPullDownRefresh()
  },
  /* 停止上拉到底部触发功能 */
  stopDownEven() {
    wx.stopPullDownRefresh()
  },
  onReachBottom() {
    // 上拉触底事件
    //console.log('触发触底事件')
  },

  onPageScroll(res) {
    // 页面滚动事件
    //console.log(res.scrollTop)
  },

  userInfo(e) {
    let _userInfo = e.detail.userInfo;
    let net = new NetWork(app.globalData._baseUrl);

    if (_userInfo) {
      this.setData({
        auth: true
      })
    }

    wx.login({
      success(res) {
        //console.log('小程序登录信息', res)
        if (res.code) {
          net._request({
            url: 'login',
            data: {
              code: res.code
            },
          }, false, sres => {

          });
        }
      }
    })
  }


})